<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="影院名称" prop="cinemaName">
            <el-input v-model="queryParams.cinemaName" placeholder="请输入" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item label="电影地址" prop="cinemaAddress">
            <el-input v-model="queryParams.cinemaAddress" placeholder="请输入" clearable style="width: 200px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <el-button @click="helloApiBtn">测试api</el-button>
            <el-button @click="queryAllCinemaBtn">查询所有影院</el-button>
            <el-button @click="queryCinemaByConditionBtn">根据条件查询影院</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
               v-hasPermi="['system:notice:add']">新增</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
               v-hasPermi="['system:notice:edit']">修改</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
               v-hasPermi="['system:notice:remove']">删除</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="queryCinemaPageList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="cinemaList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="序号" prop="cinemaId" width="100" align="center" />
         <el-table-column label="城市" prop="cinemaCity"  width="100" align="center" :show-overflow-tooltip="true" />
         <el-table-column label="影院名称" prop="cinemaName" align="center" />
         <el-table-column label="影院地址" prop="cinemaAddress" align="center" />
         <el-table-column label="创建时间" prop="createTime" width="180" align="center" />
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="getMovieList(scope.row)"
                  v-hasPermi="['system:notice:edit']">电影</el-button>
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:notice:edit']">修改</el-button>
               <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['system:notice:remove']">删除</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize" @pagination="queryCinemaPageList" />

      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" v-model="open" width="780px" append-to-body>
         <el-form ref="cinemaRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="城市" prop="cinemaCity">
               <el-input v-model="form.cinemaCity" placeholder="请输入城市" />
            </el-form-item>
            <el-form-item label="影院名称" prop="cinemaName">
               <el-input v-model="form.cinemaName" placeholder="请输入影院名称" />
            </el-form-item>
            <el-form-item label="影院地址" prop="cinemaAddress">
               <el-input v-model="form.cinemaAddress" placeholder="请输入影院地址" />
            </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="apitest1">
import { helloApi, queryAllCinema, queryCinemaByCondition, queryCinemaByPage, addCinema, updateCinema, deleteCinema } from "@/api/apitest/cinema";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict("sys_notice_status", "sys_notice_type");

const cinemaList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

let mySelection = []

const router = useRouter();

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
   },
   /* rules: {
      noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
      noticeType: [{ required: true, message: "公告类型不能为空", trigger: "change" }]
   }, */
});

const { queryParams, form, rules } = toRefs(data);

/** 取消按钮 */
function cancel() {
   open.value = false;
   reset();
}

/** 表单重置 */
function reset() {
   form.value = {
      /* noticeId: undefined,
      noticeTitle: undefined,
      noticeType: undefined,
      noticeContent: undefined,
      status: "0" */
   };
   proxy.resetForm("cinemaRef");
}

/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   queryCinemaPageList();
}

/** 重置按钮操作 */
function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.cinemaId);
   single.value = selection.length != 1;
   multiple.value = !selection.length;

   mySelection = selection
}

/** 新增按钮操作 */
function handleAdd() {
   reset();
   open.value = true;
   title.value = "添加";
}

/**修改按钮操作 */
function handleUpdate(row) {
   reset();
   title.value = "修改"

   if(row.cinemaId) {
      form.value = JSON.parse(JSON.stringify(row))
   } else {
      form.value = JSON.parse(JSON.stringify(mySelection[0]))
   }

   open.value = true
}

/** 提交按钮 */
function submitForm() {
   proxy.$refs["cinemaRef"].validate(valid => {
      if (valid) {
         if (form.value.cinemaId != undefined) {
            updateCinema(form.value).then(response => {
               proxy.$modal.msgSuccess("修改成功");
               open.value = false;
               queryCinemaPageList();
            });
         } else {
            addCinema(form.value).then(response => {
               proxy.$modal.msgSuccess("新增成功");
               open.value = false;
               queryCinemaPageList();
            });
         }
      }
   });
}

// 接口测试
const helloApiBtn = () => {
   helloApi().then(response => {
      console.log(response);
      proxy.$modal.msgSuccess("测试成功");
   });
}

// 查询所有影院
const queryAllCinemaBtn = () => {
   loading.value = true;
   queryAllCinema().then(response => {
      cinemaList.value = response.data;
      loading.value = false;
   });
}

const queryCinemaByConditionBtn = () => {
   loading.value = true;

   let params = {
      ...queryParams.value,
   }

   queryCinemaByCondition(params).then(response => {
      loading.value = false;
      cinemaList.value = response;
   });
}

// 分页查询影院
const queryCinemaPageList = () => {
   loading.value = true;

   let params = {
      ...queryParams.value,
   }

   queryCinemaByPage(params).then(response => {
      cinemaList.value = response.rows;
      total.value = response.total;
      loading.value = false;
   });
}

const getMovieList = (row) => {
   router.push({
      path: "/apitest/cinema/movie",
      query: {
         cinemaId: row.cinemaId,
      }
   });
}

/** 删除按钮操作 */
function handleDelete(row) {
   const cinemaIds = row.cinemaId || ids.value
   proxy.$modal.confirm('是否确认删除编号为"' + cinemaIds + '"的数据项？').then(function () {
      return deleteCinema(cinemaIds);
   }).then(() => {
      queryCinemaPageList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}

queryCinemaPageList();
</script>
